package com.example.filter;

import com.alibaba.fastjson.JSONObject;
import com.example.common.CommonResult;
import com.example.constant.Constants;
import com.example.exception.ExceptionCodeEnum;
import com.example.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebFilter(urlPatterns = {"/front/user/*", "/front/house/*", "/front/chat/*"})
public class FrontLoginFilter implements Filter {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        // 类型转换用于获取请求头
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 如果是登录则不拦截
        if (request.getRequestURI().equals("/front/user/login")
                || request.getRequestURI().equals("/front/user/register")
                || request.getRequestURI().equals("/front/user/getVerifyCode")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        // 如果是获取首页房源列表则不拦截
        if (request.getRequestURI().equals("/front/house/list")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        String token = request.getHeader(Constants.HEADER_AUTHORIZATION_KEY);

        if (StringUtils.hasLength(token)) {
            long expire = redisUtil.getExpire(Constants.HEAD_FRONT_TOKEN + token);
            if (expire > 0) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                String string = JSONObject.toJSONString(CommonResult.failed(ExceptionCodeEnum.UNAUTHORIZED, "当前登录已过期!"));
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token,Appid,Secret,Authorization");
                response.setContentType("application/json;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.write(string);
            }
        } else {
            String string = JSONObject.toJSONString(CommonResult.failed(ExceptionCodeEnum.UNAUTHORIZED, "当前页面需要登录"));
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token,Appid,Secret,Authorization");
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(string);
        }

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
